package com.example.config;

import cn.hutool.core.thread.NamedThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.*;

/**
 * spring事件订阅发布配置
 * @author long
 * @date 2024/9/3 16:34
 */
@Configuration
public class EventPublishConfig {

    @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    public SimpleApplicationEventMulticaster myEventMulticaster(){
        SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
        simpleApplicationEventMulticaster.setTaskExecutor(taskExecutor());
        return simpleApplicationEventMulticaster;
    }

    @Bean
    public Executor taskExecutor() {
        BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(100);
        return new ThreadPoolExecutor(5,10,60, TimeUnit.SECONDS,
                blockingQueue,new ThreadPoolExecutor.DiscardPolicy());
    }
}
